import {
    View,
    Text,
    StyleSheet,
    StatusBar,
    Image,
    Animated,
    Easing,
    Linking,
    AppState,
    AppStateStatus
} from 'react-native'
import React, {Component} from 'react'
import {Actions} from "react-native-router-flux";
import styles, {screenHeight, screenWidth} from "../style";
import {inject} from "mobx-react";
import {Store} from "../store";
import {IS_IOS} from "../style/constant";
import {alertProxy} from "../component/common/AlertProxy";
import {needUpdate} from "../config";

interface WelcomeProps extends Store {

}

interface WelcomeState {
}

@inject("user", "main")
export default class Welcome extends Component<WelcomeProps, WelcomeState> {

    constructor(props: WelcomeProps) {
        super(props);
    }


    componentDidMount() {
        const animateTime = 2000;
        //老版本更新
        if (needUpdate) {
            this.handleAppStateChange("active");
            AppState.addEventListener('change', this.handleAppStateChange);
        } else {
            this.props.main.getCategories();
            if (IS_IOS) {
                setTimeout(() => {
                    this.goNext();
                }, animateTime)
            } else {
                setTimeout(() => {
                    this.goNext();
                }, animateTime)
            }
        }
    }

    async goNext() {
        const {user} = this.props;
        if (user.user) {
            Actions.reset("RootTabs");
        } else {
            Actions.reset("Login");
        }
    }

    //TODO更新
    handleAppStateChange = (nextAppState: AppStateStatus) => {
        if (nextAppState === "active") {
            alertProxy("提示", "请更新版本!", [{
                text: "我知道了",
                onPress: () => {
                    const url = IS_IOS ? "https://itunes.apple.com/cn/app/%E5%82%BB%E7%B2%AE%E5%95%86%E5%9F%8E/id1425948410?mt=8" : "https://www.pgyer.com/qV3I";
                    Linking.canOpenURL(url).then(supported => {
                        if (!supported) {
                            global.showError("不支持打开链接!");
                        } else {
                            return Linking.openURL(url);
                        }
                    }).catch(err => global.showError("不支持打开链接！"));
                }
            }])
        }
    };

    componentWillUnmount() {
        //TODO更新
        needUpdate && AppState.removeEventListener('change', this.handleAppStateChange);
    }


    render() {
        return (<View style={[styles.mainBox, {backgroundColor: "#fff", paddingBottom: "10%"}]}>
            <View style={[styles.centered, {flex: 1}]}>
                <Image source={require("../assets/img/qidongtu.jpg")}
                       resizeMode={"stretch"}
                       style={{width: screenWidth, height: screenHeight}}/>
                {/*{IS_IOS ? <View style={[styles.absoluteFull, styles.centered, {justifyContent: "flex-end"}]}>*/}
                {/*<View style={[styles.centered, {width: 150, height: 150}]}>*/}
                {/*<LottieView*/}
                {/*ref={lottieView => this.lottieView = lottieView}*/}
                {/*style={{*/}
                {/*width: 150,*/}
                {/*height: 150,*/}
                {/*}}*/}
                {/*source={require('../style/lottie/newAnimation.json')}*/}
                {/*progress={this.state.progress}*/}
                {/*/>*/}
                {/*</View>*/}
                {/*</View> : null}*/}
            </View>
        </View>)
    }
}

const WelcomeStyle = StyleSheet.create({
    container: {}
});

 